package com.hll.api.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.Random;

import javax.imageio.ImageIO;

import org.apache.commons.lang3.RandomUtils;

import com.alibaba.druid.util.Base64;


/**
 *
 * @ClassName: ValidateCodeUtil
 * @Description: 验证码生成工具类
 * @author chenhx
 * @date 2017年11月14日 上午11:00:07
 */
public class ValidateCodeUtil {
	private static Validate validate = null;                  //验证码类，用于最后返回此对象，包含验证码图片base64和真值
	private static Random random = new Random();              //随机类，用于生成随机参数
	private static String randString = "0123456789abcdefghijkmnpqrtyABCDEFGHIJLMNQRTY";//随机生成字符串的取值范围

	private static final int WIDTH = 80;     //图片宽度
	private static final int HEIGHT = 34;    //图片高度
	private static final int STRING_NUM = 4;  //字符的数量
	private static final int LINE_SIZE = 20;  //干扰线数量


	//将构造函数私有化 禁止new创建
	private ValidateCodeUtil() {
		super();
	}

	/**
	 * 获取随机字符,并返回字符的String格式
	 * @param index (指定位置)
	 */
	public static String getRandomChar(int index) {
		//获取指定位置index的字符，并转换成字符串表示形式
		return String.valueOf(randString.charAt(index));
	}
	/**
	 * 获取随机指定区间的随机数
	 */
	private static int getRandomNum() {
		return RandomUtils.nextInt(5, 10);
	}

	/**
	 * 获得字体
	 */
	private static Font getFont() {
		return new Font("Fixedsys", Font.BOLD, 25);  //名称、样式、磅值
	}

	/**
	 * 获得颜色
	 */
	private static Color getRandColor(int frontColor, int backColor) {
		if(frontColor > 255) {
			frontColor = 255;
		}
		if(backColor > 255) {
			backColor = 255;
		}

		int red = frontColor + random.nextInt(backColor - frontColor - 16);
		int green = frontColor + random.nextInt(backColor - frontColor -14);
		int blue = frontColor + random.nextInt(backColor - frontColor -18);
		return new Color(227, 75, 70);
	}

	/**
	 * 绘制字符串,返回绘制的字符串
	 */
	private static String drawString(Graphics g, String randomString, int i) {
		Graphics2D g2d = (Graphics2D) g;
		g2d.setFont(getFont());   //设置字体
		g2d.setColor(new Color(random.nextFloat(), random.nextFloat(), random.nextFloat()));//设置颜色
		String randChar = getRandomChar(random.nextInt(randString.length()));
		randomString += randChar;   //组装
		int rot = getRandomNum();
		g2d.translate(random.nextInt(3), random.nextInt(3));
		g2d.rotate(rot * Math.PI / 180);
		g2d.drawString(randChar, 13*i, 20);
		g2d.rotate(-rot * Math.PI / 180);
		return randomString;
	}

	/**
	 * 绘制干扰线
	 */
	private static void drawLine(Graphics g) {
		//起点(x,y)  偏移量x1、y1
		int x = random.nextInt(WIDTH);
		int y = random.nextInt(HEIGHT);
		int xl = random.nextInt(13);
		int yl = random.nextInt(15);
		g.setColor(new Color(random.nextFloat(), random.nextFloat(), random.nextFloat()));
		g.drawLine(x, y, x + xl, y + yl);
	}

	/**
	 *
	 * @MethodName: getRandomCode
	 * @Description: 生成Base64图片验证码
	 * @return String 返回base64
	 */
	public static Validate getRandomCode() {
		validate = validate==null?new Validate():validate;

		// BufferedImage类是具有缓冲区的Image类,Image类是用于描述图像信息的类
		BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_BGR);
		Graphics g = image.getGraphics();// 获得BufferedImage对象的Graphics对象
		g.fillRect(0, 0, WIDTH, HEIGHT);//填充矩形
		g.setFont(new Font("Times New Roman", Font.PLAIN, 18));//设置字体
		g.setColor(getRandColor(110, 133));//设置颜色
		//绘制干扰线
		for(int i = 0; i <= LINE_SIZE; i++) {
			drawLine(g);
		}
		//绘制字符
		String randomString = "";
		for(int i = 1; i <= STRING_NUM; i++) {
			randomString = drawString(g, randomString, i);
			validate.setValue(randomString);
		}

		g.dispose();//释放绘图资源
		ByteArrayOutputStream bs = null;
		try {
			bs = new ByteArrayOutputStream();
			//将绘制得图片输出到流
			ImageIO.write(image, "png", bs);
			String imgsrc = Base64.byteArrayToBase64(bs.toByteArray());
			validate.setBase64Str(imgsrc);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				assert bs != null;
				bs.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return validate;
	}

	/**
	 *
	 * @ClassName: Validate
	 * @Description: 验证码类
	 * @author chenhx
	 * @date 2017年11月14日 上午11:35:34
	 */
	public static class Validate implements Serializable{
		private static final long serialVersionUID = 1L;
		private String base64Str;		//Base64 值
		private String value;			//验证码值

		public String getBase64Str() {
			return base64Str;
		}
		public void setBase64Str(String base64Str) {
			this.base64Str = base64Str;
		}
		public String getValue() {
			return value;
		}
		public void setValue(String value) {
			this.value = value;
		}
	}
}

